package com.tomcat.myapplication.http;

import android.content.Context;

import com.tomcat.myapplication.http.constants.ConstantExceptionString;
import com.tomcat.myapplication.http.exception.HttpException;
import com.tomcat.myapplication.http.i_http.HttpProgressCancelListener;
import com.tomcat.myapplication.http.i_http.HttpSubscriberOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import rx.Subscriber;


/**
 * 用于在Http请求开始时，自动显示一个ProgressDialog
 * 在Http请求结束是，关闭ProgressDialog
 * 调用者自己对请求数据进行处理
 */
public class HttpProgressSubscriber<T> extends Subscriber<T> implements HttpProgressCancelListener {

    private HttpSubscriberOnNextListener mHttpSubscriberOnNextListener;
    private HttpProgressDialogHandler mHttpProgressDialogHandler;
    private Context mContext;

    public HttpProgressSubscriber(HttpSubscriberOnNextListener mHttpSubscriberOnNextListener, Context context, boolean show) {

        this.mHttpSubscriberOnNextListener = mHttpSubscriberOnNextListener;
        this.mContext = context;
        mHttpProgressDialogHandler = new HttpProgressDialogHandler(context, this, true, show);
    }

    /**
     * 显示ProgressDialog
     *
     */

    private void showProgressDialog() {
        if (mHttpProgressDialogHandler != null) {
            mHttpProgressDialogHandler.obtainMessage(HttpProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    /**
     * 关闭ProgressDialog
     *
     */
    private void dismissProgressDialog() {
        if (mHttpProgressDialogHandler != null) {
            mHttpProgressDialogHandler.obtainMessage(HttpProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mHttpProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     *
     */
    @Override
    public void onStart() {
        showProgressDialog();
    }

    /**
     * 完成，隐藏ProgressDialog
     *
     */

    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     *
     */
    @Override
    public void onError(Throwable e) {
        if (e instanceof SocketTimeoutException) {
            if (mHttpSubscriberOnNextListener != null) {
                mHttpSubscriberOnNextListener.onError("网络中断，请检查您的网络状态");
            }
        } else if (e instanceof ConnectException) {
            if (mHttpSubscriberOnNextListener != null) {
                mHttpSubscriberOnNextListener.onError("网络中断，请检查您的网络状态");
            }
        } else if(e instanceof HttpException){
            if (mHttpSubscriberOnNextListener != null) {
                //自定义异常
                if(ConstantExceptionString.CONSTANT_EXCEPTION_ONE.equals(((HttpException) e).getErrCode())){
                    mHttpSubscriberOnNextListener.onError(((HttpException) e).getMessage());
                }else if(ConstantExceptionString.CONSTANT_EXCEPTION_tWO.equals(((HttpException) e).getErrCode())){
                    mHttpSubscriberOnNextListener.onError(((HttpException) e).getMessage());
                }else if(ConstantExceptionString.CONSTANT_EXCEPTION_THREE.equals(((HttpException) e).getErrCode())){
                    mHttpSubscriberOnNextListener.onError(((HttpException) e).getMessage());
                }
            }
        }else{
            if (mHttpSubscriberOnNextListener != null) {
                mHttpSubscriberOnNextListener.onError(e.getMessage());
            }
        }

        dismissProgressDialog();
    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {

        if (mHttpSubscriberOnNextListener != null) {
            mHttpSubscriberOnNextListener.onNext(t);
        }
    }


    /**
        * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
                *
        */
        @Override
        public void onCancelProgress() {
            if (!this.isUnsubscribed()) {
                this.unsubscribe();
            }
    }
}